<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="800px"
    top="10px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >

      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="dataForm.nickName"
          placeholder="请输入用户昵称"
        ></el-input>
      </el-form-item>

      <el-form-item label="用户头像" prop="headerUrl">
        <ImageUploadSingle v-model="dataForm.headerUrl"></ImageUploadSingle>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="商品信息:" prop="spuId">
            <el-select
              v-model="dataForm.spuId"
              filterable
              placeholder="请选择商品信息"
              clearable
              size="small"
              class="margin-left-xs"
              style="width:100%"
            >
              <el-option
                v-for="dict in goodsSpuList"
                :key="dict.id"
                :label="dict.spuTitle"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- <el-form-item label="商品Sku:" prop="skuId">
            <el-select
              v-model="dataForm.skuId"
              filterable
              placeholder="请选择商品Sku"
              clearable
              size="small"
              class="margin-left-xs"
              style="width:100%"
            >
              <el-option
                v-for="dict in goodsSkuList"
                :key="dict.id"
                :label="dict.skuShortName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="购买数量" prop="buyCount">
            <el-input
              v-model="dataForm.buyCount"
              placeholder="请输入购买数量"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="商品评分" prop="goodsScore">
            <div style="margin-top:6px">
              <el-rate v-model="dataForm.goodsScore"></el-rate>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务评分" prop="serverScore">
            <div style="margin-top:6px">
              <el-rate v-model="dataForm.serverScore"></el-rate>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="物流评分" prop="deliveryScore">
            <div style="margin-top:6px">
              <el-rate v-model="dataForm.deliveryScore"></el-rate>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          
        </el-col>
      </el-row>
      <el-form-item label="评论图片" prop="userFirstComment.commentImages">
        <ImagesUpload v-model="dataForm.userFirstComment.commentImages"></ImagesUpload>
      </el-form-item>
      <el-form-item label="评论内容" prop="userFirstComment.content">
        <el-input type="textarea" :rows="4" v-model="dataForm.userFirstComment.content" placeholder="评论内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_order/order_comment";
import ImageUploadSingle from '@/components/imageUploadSingle';
import ImagesUpload from "@/components/imagesUpload";
import {getGoodsSpuList} from '@/api/shop_goods/spu';
import {getGoodsSkuBySpuId} from '@/api/shop_goods/sku';

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        nickName:'',
        headerUrl:null,
        spuId:'',
        skuId:'',
        buyCount:'',
        goodsScore:5,
        serverScore:5,
        deliveryScore:5,
        userFirstComment:{
          commentImages:[],
          content:'',
        }
      },
      dataRule: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        headerUrl: [
          { required: true, message: "用户头像不能为空", trigger: "blur" },
        ],
        spuId: [
          { required: true, message: "商品信息不能为空", trigger: "blur" },
        ],
        skuId: [
          { required: true, message: "商品Sku不能为空", trigger: "blur" },
        ],
        buyCount: [
          { required: true, message: "购买数量不能为空", trigger: "blur" },
        ],
        goodsScore: [
          { required: true, message: "商品评分不能为空", trigger: "blur" },
        ],
        serverScore: [
          { required: true, message: "服务评分不能为空", trigger: "blur" },
        ],
        deliveryScore: [
          { required: true, message: "物流评分不能为空", trigger: "blur" },
        ],
        'userFirstComment.content': [
          { required: true, message: "评论内容不能为空", trigger: "blur" },
        ]
      },
      goodsSpuList:[],
      goodsSkuList:[]
    };
  },
  components: {
    ImageUploadSingle,
    ImagesUpload
  },
  watch:{

    /**
     * 商品Spu发生变化
     */
    // 'dataForm.spuId':function(val){
    //   this.dataForm.skuId = ''
    //   this.getGoodsSkuBySpuId(val)
    // }
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      this.getGoodsSpuList()
      if(id){
        getObj(id).then((response) => {
          let data = response.data.data;
          data.headerUrl = {
            materialId: '',
            materialUrl: data.headerUrl
          }
          if(data.userFirstComment){
            if(data.userFirstComment.commentImages){
              data.userFirstComment.commentImages = data.userFirstComment.commentImages.split(',')
            } else {
              data.userFirstComment.commentImages = []
            }
          }
          this.dataForm = data
        });
      }
    },

    /**
     * 获取所有的商品spu
     */
    getGoodsSpuList(){
      getGoodsSpuList().then(res=>{
        this.goodsSpuList = res.data.data
      })
    },

    /**
     * 根据spuId获取sku列表
     */
    getGoodsSkuBySpuId(spuId){
      getGoodsSkuBySpuId({spuId}).then(res=>{
        this.goodsSkuList = res.data.data
      })
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          let requestData = JSON.parse(JSON.stringify(this.dataForm))
          if(requestData.userFirstComment.commentImages && requestData.userFirstComment.commentImages.length > 0){
            requestData.userFirstComment.commentImages = requestData.userFirstComment.commentImages.map(p=>p.materialUrl).join()
          } else {
            requestData.userFirstComment.commentImages = ''
          }
          
          let skus = this.goodsSkuList.filter(p=>p.id == requestData.skuId)
          if(skus.length > 0){
            requestData.skuName = skus[0].skuName
            requestData.skuShortName = skus[0].skuShortName
            requestData.sellPrice = skus[0].sellPrice
          }
          requestData.headerUrl = requestData.headerUrl.materialUrl
          let spus = this.goodsSpuList.filter(p=>p.id == requestData.spuId)
          if(spus.length > 0){
            requestData.spuTitle = spus[0].spuTitle
          }
          console.log(requestData)
          
          if (requestData.id) {
            putObj(requestData)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(requestData)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
